package com.self.educenter.controller;


import com.self.commonutils.JwtUtils;
import com.self.commonutils.R;
import com.self.commonutils.ordervo.UcenterMemberOrder;
import com.self.educenter.entity.UcenterMember;
import com.self.educenter.entity.vo.RegisterVo;
import com.self.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author xiaozeng
 * @since 2020-10-04
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){

        //调用service方法实现登录
        //返回token,使用JWT生成出来
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){

        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){

        //调用JWT工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据Id查询数据库，获取用户信息
        UcenterMember member = memberService.getById(memberId);

        return R.ok().data("userInfo",member);
    }

    //根据用户Id获取用户信息
    @PostMapping("getUserInfoOrder/{userId}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("userId") String userId){

        UcenterMember member = memberService.getById(userId);
        //将member的值赋给UcenterMemberOrder对象
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,memberOrder);

        return memberOrder;
    }

    //查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day){

        Integer count = memberService.countRegisterDay(day);

        return R.ok().data("countRegister",count);
    }


}

